/*
 * Main.java
 *
 * Created on December 2, 2007, 4:31 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.xeerkat.xmpp.server.tools;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import org.infoset.xml.XMLException;
import org.restlet.Component;
import org.restlet.data.Protocol;
import org.xeerkat.xmpp.server.Host;
import org.xeerkat.xmpp.server.XMPPServer;
import org.xeerkat.xmpp.server.db.XMPPDB;
import org.xeerkat.xmpp.server.www.AdminComponent;

/**
 *
 * @author alex
 */
public class Main
{

   ServerConfiguration conf;
   XMPPDB database;
   XMPPServer  server;
   Component webAdmin;
   
   /** Creates a new instance of Main */
   public Main(URI location)
      throws IOException,XMLException
   {
      System.out.println("Version "+XMPPServer.class.getPackage().getImplementationVersion());
      
      conf = new ServerConfiguration();
      conf.load(location);
      webAdmin = null;

   }
   
   public void init() {
      URI databaseRef = conf.getDatabaseRef();
      if (!databaseRef.getScheme().equals("file")) {
         throw new IllegalArgumentException("Non-embedded databases are not supported.");
      }
      File dbDir = new File(databaseRef.getSchemeSpecificPart());
      database = new XMPPDB("jdbc:derby:"+dbDir.getAbsolutePath()+";create=true");

      server = new XMPPServer(database);
      for (ServerConfiguration.Interface iface : conf.getInterfaces()) {
         server.bind(iface.getAddress(),iface.getPort(),iface.getSecurePort());
      }
      if (conf.getKeystoreFile()!=null) {
         server.setKeystorePath(conf.getKeystoreFile());
         server.setKeystorePassword(conf.getKeystorePassword());
         server.setKeyPassword(conf.getKeyPassword());
      }
      for (ServerConfiguration.Host confHost : conf.getHosts().values()) {
         server.getHosts().put(confHost.getName(),new Host(confHost.getName(),confHost.getAuthService()));
      }
      server.setAutoConfigurationWait(conf.getAutoConfigurationCheckWait());
      server.getAutoConfiguration().addAll(conf.getAutoConfiguration());
     
      if (conf.getAdminHost()!=null) {
         webAdmin = new AdminComponent(Protocol.HTTP,conf.getAdminHost(),conf.getAdminPort(),conf.getAdminAuthService(),server);
      }
   }
   
   public void run() 
      throws Exception
   {
      database.connect();
      server.start();
      if (webAdmin!=null) {
         webAdmin.start();
      }
   }
   
   public void stop() 
      throws Exception
   {
      server.stop();
      if (webAdmin!=null) {
         webAdmin.stop();
      }
   }
   
   public static void main(String [] args) {
      try {
         if (args.length!=1) {
            System.err.println("Usage: "+Main.class.getName()+" conf-file");
            System.exit(1);
         }
         
         URI location = args[0].charAt(0)=='!' ? URI.create(args[0].substring(1)) : (new File(args[0])).toURI();
         Main main = new Main(location);
         main.init();
         main.run();
         
      } catch (FileNotFoundException ex) {
         System.err.println(ex.getMessage());
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
   
}
